﻿Imports Autodesk.AutoCAD.Colors
Public Class CLDS_Route_Container
    Private pCA As CLDS_ACAD
    Private pCRColour As System.Drawing.Color
    Private pARColour As System.Drawing.Color
    Private pPS As Autodesk.AutoCAD.Windows.Palette = Nothing
    Private pSD As CLDS_Segment_Container = Nothing
    Private pARPrep As Boolean = True
    Private qbeCCableID As String = ""
    Private qbeCDeviceID As String = ""
    Private qbeCNodeID As String = ""
    Private qbeCType As String = ""
    Private qbeCSeg As String = ""
    Private qbeFRouted As String = "-"
    Private qbeFARouted As String = "-"
    Private qbeFOffroute As String = "-"
    Private qbeVessel As String = "-"
    Private qbeSegment As String = ""
    Private pVessel As Integer = 1
    Private vFOffL As String
    Private vTOffL As String
    Friend WithEvents cbFNode As ComboBox
    Friend WithEvents cbTNode As ComboBox
    Friend WithEvents cbCType As ComboBox
    Friend WithEvents cbCSeg As ComboBox

    Public Property CA() As CLDS_ACAD
        Get
            Return pCA
        End Get
        Set(ByVal value As CLDS_ACAD)
            pCA = value
        End Set
    End Property
    Public Property CRColour() As System.Drawing.Color
        Get
            Return pCRColour
        End Get
        Set(ByVal value As System.Drawing.Color)
            pCRColour = value
        End Set
    End Property
    Public Property ARColour() As System.Drawing.Color
        Get
            Return pARColour
        End Get
        Set(ByVal value As System.Drawing.Color)
            pARColour = value
        End Set
    End Property
    Public Property PSParent() As Autodesk.AutoCAD.Windows.Palette
        Get
            Return pPS
        End Get
        Set(ByVal value As Autodesk.AutoCAD.Windows.Palette)
            pPS = value
        End Set
    End Property
    Public Property SegmentDialog() As CLDS_Segment_Container
        Get
            Return pSD
        End Get
        Set(ByVal value As CLDS_Segment_Container)
            pSD = value
        End Set
    End Property
    Public Property ARPrep() As Boolean
        Get
            Return pARPrep
        End Get
        Set(ByVal value As Boolean)
            pARPrep = value
        End Set
    End Property
    Public Property pcbFNode() As ComboBox
        Get
            Return cbFNode
        End Get
        Set(ByVal value As ComboBox)
            cbFNode = value
        End Set
    End Property
    Public Property ptbFOffL() As String
        Get
            Return vFOffL
        End Get
        Set(ByVal value As String)
            vFOffL = value
        End Set
    End Property
    Public Property pcbTNode() As ComboBox
        Get
            Return cbTNode
        End Get
        Set(ByVal value As ComboBox)
            cbTNode = value
        End Set
    End Property
    Public Property ptbTOffL() As String
        Get
            Return vTOffL
        End Get
        Set(ByVal value As String)
            vTOffL = value
        End Set
    End Property
    Public Property pcbCType() As ComboBox
        Get
            Return cbCType
        End Get
        Set(ByVal value As ComboBox)
            cbCType = value
        End Set
    End Property
    Public Property pcbCSeg() As ComboBox
        Get
            Return cbCSeg
        End Get
        Set(ByVal value As ComboBox)
            cbCSeg = value
        End Set
    End Property
    Public Property Vessel() As Integer
        Get
            Return pVessel
        End Get
        Set(ByVal value As Integer)
            pVessel = value
        End Set
    End Property
    Private Sub cbSelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        ' MessageBox.Show("Changed " & cb.Name & " to " & cb.Text)
        If (Me.cbFNode.Text = Me.tbFromNode.Text _
                And Me.cbTNode.Text = Me.tbToNode.Text _
                And Me.cbCType.Text = Me.tbType.Text _
                And Me.cbCSeg.Text = Me.tbSeg.Text) Then
            Me.btCabSave.Enabled = False
        Else
            Me.btCabSave.Enabled = True
        End If
    End Sub
    Public Sub Initialize(ByVal p_CA As CLDS_ACAD, ByVal p_SD As CLDS_Segment_Container)
        cbFNode = New ComboBox
        cbFNode.Size = tbFromNode.Size
        cbFNode.Location = tbFromNode.Location
        cbFNode.AutoCompleteMode = AutoCompleteMode.Suggest
        Me.gbCable.Controls.Add(cbFNode)
        cbFNode.BringToFront()
        cbFNode.Hide()
        AddHandler cbFNode.TextChanged, AddressOf cbSelectedIndexChanged
        AddHandler cbFNode.SelectedIndexChanged, AddressOf cbSelectedIndexChanged
        cbTNode = New ComboBox
        cbTNode.Size = tbToNode.Size
        cbTNode.Location = tbToNode.Location
        Me.gbCable.Controls.Add(cbTNode)
        cbTNode.BringToFront()
        cbTNode.Hide()
        AddHandler cbTNode.SelectedIndexChanged, AddressOf cbSelectedIndexChanged
        cbCType = New ComboBox
        cbCType.Size = tbType.Size
        cbCType.Location = tbType.Location
        Me.gbCable.Controls.Add(cbCType)
        cbCType.BringToFront()
        cbCType.Hide()
        AddHandler cbCType.SelectedIndexChanged, AddressOf cbSelectedIndexChanged
        cbCSeg = New ComboBox
        cbCSeg.Size = tbSeg.Size
        cbCSeg.Location = tbSeg.Location
        Me.gbCable.Controls.Add(cbCSeg)
        cbCSeg.BringToFront()
        cbCSeg.Hide()
        AddHandler cbCSeg.SelectedIndexChanged, AddressOf cbSelectedIndexChanged
        Me.pCA = p_CA
        Me.pSD = p_SD
        Me.pVessel = Val(pCA.pProps.v_focus)
        If p_CA.pTokens("PD_ANALYSIS") = "N" Then
            Me.cbMRAnalysis.Visible = False
            Me.cbMRAnalysis.Checked = False
        Else
            Me.cbMRAnalysis.Visible = True
        End If
    End Sub
    Public Sub Reinitialize(ByVal p_CA As CLDS_ACAD)
        Me.tbCabID.Enabled = True
        Me.btExeqry.Visible = True
        Me.CRColour = p_CA.pPrefs.c_crte
        Me.btCRColour.BackColor = Me.CRColour
        Me.ARColour = p_CA.pPrefs.c_arte
        Me.btARColour.BackColor = Me.ARColour
        Me.pVessel = Val(pCA.pProps.v_focus)
        p_CA.CLDS_RouteCableDetail(Me, "")
        If p_CA.pTokens("PD_ANALYSIS") = "N" Then
            Me.cbMRAnalysis.Visible = False
            Me.cbMRAnalysis.Checked = False
        Else
            Me.cbMRAnalysis.Visible = True
        End If
    End Sub
    Private Sub btCRShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRShow.Click
        Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
        pCA.CLDS_RouteHilite(Me, Me.tbCabID.Text, c, Me.lvCRSegments)
    End Sub

    Private Sub btCRClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRClear.Click
        pCA.CLDS_RouteClear(Me, Me.tbCabID.Text)
    End Sub
    Public Sub ShowVessel()
        Me.gbCable.Text = "Cable details"
        If pCA.pCMAPI.CMUnits > 1 Then
            Me.gbCable.Text = Me.gbCable.Text & " [" & pCA.pCMAPI.CMUnitdesc & " " & Trim(Str$(Me.pVessel)) & "]"
        End If
        Me.gbCable.Update()
    End Sub
    Public Sub ShowCable()
        Dim cabid As String = Me.tbCabID.Text
        pCA.CLDS_RouteCableDetail(Me, cabid, Me.pVessel)
        Me.tbCabID.ReadOnly = True
    End Sub
    Private Sub btExeqry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExeqry.Click
        Dim cabid As String = Me.tbCabID.Text
        Dim vessel As Integer = Val(pCA.pProps.v_focus)
        If cabid = "" Then
            If Not pSD Is Nothing Then
                If pSD.lvCables.SelectedItems.Count = 1 Then
                    Dim lvi As ListViewItem = pSD.lvCables.SelectedItems.Item(0)
                    cabid = lvi.Text
                    vessel = lvi.SubItems(1).Text
                End If
            End If
        End If
        Dim qfv As Boolean
        Dim qbeID As String = cabid
        If Not Me.tbCabID.ReadOnly Then
            qbeCCableID = Me.tbCabID.Text
            qbeCDeviceID = Me.tbFromDev.Text
            qbeCNodeID = Me.tbFromNode.Text
            qbeCType = Me.tbType.Text
            qbeCSeg = Me.tbSeg.Text
            qbeFRouted = Me.cbQBERouted.Text
            qbeFARouted = Me.cbQBEArouted.Text
            qbeFOffroute = Me.cbQBEOffroute.Text
            qbeVessel = Me.cbQBEVessel.Text
            qbeSegment = Me.tbQBESegment.Text
            Me.tbCabID.ReadOnly = True
            Me.tbFromDev.ReadOnly = True
            Me.tbFromNode.ReadOnly = True
            Me.tbType.ReadOnly = True
            Me.tbSeg.ReadOnly = True
            If InStr(qbeCCableID, "%") > 0 Or InStr(qbeCCableID, "_") > 0 _
            Or qbeCDeviceID <> "" _
            Or qbeCNodeID <> "" _
            Or qbeCType <> "" _
            Or qbeCSeg <> "" _
            Or qbeFRouted <> "-" _
            Or qbeFARouted <> "-" _
            Or qbeFOffroute <> "-" _
            Or qbeVessel = "-" _
            Or qbeSegment <> "" Then
                qbeID = ""
                If qbeCCableID <> "" Then
                    qbeID = qbeID + " CABID=""" + qbeCCableID + """"
                End If
                If qbeCDeviceID <> "" Then
                    qbeID = qbeID + " DEVID=""" + qbeCDeviceID + """"
                End If
                If qbeCNodeID <> "" Then
                    qbeID = qbeID + " NODEID=""" + qbeCNodeID + """"
                End If
                If qbeCType <> "" Then
                    qbeID = qbeID + " CTYPE=""" + qbeCType + """"
                End If
                If qbeCSeg <> "" Then
                    qbeID = qbeID + " CSEG=""" + qbeCSeg + """"
                End If
                If qbeFRouted <> "-" Then
                    qbeID = qbeID + " ROUTED=""" + qbeFRouted + """"
                End If
                If qbeFARouted <> "-" Then
                    qbeID = qbeID + " AROUTED=""" + qbeFARouted + """"
                End If
                If qbeFOffroute <> "-" Then
                    qbeID = qbeID + " OFFROUTE=""" + qbeFOffroute + """"
                End If
                If qbeSegment <> "" Then
                    qbeID = qbeID + " SEGID=""" + qbeSegment + """"
                End If
            End If
            qfv = Me.gbQBECFilter.Visible
            Me.gbQBECFilter.Visible = False
            If Me.cbQBEVessel.Text = "-" Then
                vessel = 0
            Else
                vessel = Val(Me.cbQBEVessel.Text)
            End If
        End If
        pCA.CLDS_RouteCableDetail(Me, Trim(qbeID), vessel)
        If qfv And Not pCA.pMultiCabDialog Is Nothing Then
            If Mid$(qbeFRouted, 1, 1) = "N" Or Mid$(qbeFRouted, 1, 1) = "P" Then
                pCA.pMultiCabDialog.mrToolStripButton1.Enabled = True
                pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = "Autoroute selected cables"
                pCA.pMultiCabDialog.A1 = "Autoroute selected cables"
                pCA.pMultiCabDialog.mrToolStripButton2.Enabled = False
                pCA.pMultiCabDialog.mrToolStripButton2.ToolTipText = ""
                pCA.pMultiCabDialog.A2 = ""
            ElseIf Mid$(qbeFRouted, 1, 1) = "Y" Then
                pCA.pMultiCabDialog.mrToolStripButton1.Enabled = False
                pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = ""
                pCA.pMultiCabDialog.A1 = ""
                pCA.pMultiCabDialog.mrToolStripButton2.Enabled = True
                pCA.pMultiCabDialog.mrToolStripButton2.ToolTipText = "Delete selected cable routes"
                pCA.pMultiCabDialog.A2 = "Delete selected cable routes"
            ElseIf Mid$(qbeFARouted, 1, 1) = "S" Or Mid$(qbeFARouted, 1, 1) = "P" Then
                pCA.pMultiCabDialog.mrToolStripButton1.Enabled = True
                pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = "Accept selected cable autoroutes"
                pCA.pMultiCabDialog.A1 = "Accept selected cable autoroutes"
                pCA.pMultiCabDialog.mrToolStripButton2.Enabled = True
                pCA.pMultiCabDialog.mrToolStripButton2.ToolTipText = "Reject selected cable autoroutes"
                pCA.pMultiCabDialog.A2 = "Reject selected cable autoroutes"
            ElseIf Mid$(qbeFARouted, 1, 1) = "F" Then
                pCA.pMultiCabDialog.mrToolStripButton1.Enabled = False
                pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = ""
                pCA.pMultiCabDialog.A1 = ""
                pCA.pMultiCabDialog.mrToolStripButton2.Enabled = True
                pCA.pMultiCabDialog.mrToolStripButton2.ToolTipText = "Reject selected cable autoroutes"
                pCA.pMultiCabDialog.A2 = "Reject selected cable autoroutes"
            Else
                pCA.pMultiCabDialog.mrToolStripButton1.Enabled = False
                pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = ""
                pCA.pMultiCabDialog.A1 = ""
                pCA.pMultiCabDialog.mrToolStripButton2.Enabled = False
                pCA.pMultiCabDialog.mrToolStripButton2.ToolTipText = ""
                pCA.pMultiCabDialog.A2 = ""
            End If
            pCA.pMultiCabDialog.ToolStrip1.Update()
        End If
    End Sub

    Private Sub btCRColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRColour.Click
        Dim result As DialogResult = Me.clCRColour.ShowDialog()
        If result = DialogResult.OK Then
            Me.CRColour = Me.clCRColour.Color
            Me.btCRColour.BackColor = Me.CRColour
            Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
            pCA.CLDS_RouteHilite(Me, Me.tbCabID.Text, c, Me.lvCRSegments)
        End If
    End Sub

    Private Sub btCRHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRHide.Click
        'Me.Hide()
        Dim doc As Autodesk.AutoCAD.ApplicationServices.Document
        Dim ed As Autodesk.AutoCAD.EditorInput.Editor
        doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        ed = doc.Editor
        Dim j As Integer = -1
        If Me.pPS.Name = "Route" Then
            Me.cbFNode.Hide()
            Me.cbTNode.Hide()
            Me.cbCType.Hide()
            Me.cbCSeg.Hide()
            Me.lvCRSegments.Clear()
            pCA.CLDS_RouteCableDetail(Me, "")
            Exit Sub
        End If
        ed.WriteMessage(vbLf & "Count=" & Me.pPS.PaletteSet.Count)
        ed.WriteMessage(vbLf & "Name=" & Me.pPS.Name)
        For i As Integer = 1 To pPS.PaletteSet.Count
            ed.WriteMessage(vbLf & (i - 1).ToString & "=" & pPS.PaletteSet.Item(i - 1).Name)
            If pPS.PaletteSet.Item(i - 1).Name = Me.pPS.Name Then
                j = i - 1
                Exit For
            End If
        Next
        ed.WriteMessage(vbLf)
        If j > 0 Then
            'pPS.PaletteSet.Remove(j)
            Dim psn As String = "Route"
            Dim k As Integer = 0
            For i As Integer = 1 To Me.pPS.PaletteSet.Count - 1
                If Mid$(Me.pPS.PaletteSet.Item(i).Name, 1, Len(psn)) = psn And i <> j Then
                    k = k + 1
                    If k > 1 Then
                        Me.pPS.PaletteSet.Item(i).Name = psn & " [" & k.ToString & "]"
                    Else
                        Me.pPS.PaletteSet.Item(i).Name = psn
                    End If
                End If
            Next
            pPS.PaletteSet.Remove(j)
        End If
    End Sub

    Private Sub btAllClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAllClear.Click
        Me.pCA.CLDS_RouteClearAll(Me)
        Me.pCA.CLDS_ClearHighlights()
    End Sub

    Private Sub lvCRSegments_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvCRSegments.SelectedIndexChanged
        Dim lv As ListView = DirectCast(sender, ListView)
        If lv.SelectedItems.Count = 1 Then
            Dim lvi As ListViewItem = lv.SelectedItems.Item(0)
            CA.CLDS_SelectSegment(lvi.SubItems.Item(2).Text, lvi.SubItems.Item(3).Text)
        End If
    End Sub

    Private Sub btEntqry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEntqry.Click
        If Me.tbCabID.ReadOnly Then
            Me.tbCabID.ReadOnly = False
            Me.tbCabID.Text = ""
            Me.tbFromDev.ReadOnly = False
            Me.tbFromNode.ReadOnly = False
            Me.tbType.ReadOnly = False
            Me.tbSeg.ReadOnly = False
            If Not pCA.pHzPalette Is Nothing Then
                pCA.pHzPalette.Visible = False
                pCA.pMultiCabDialog.lvMultiCable.Clear()
            End If
            pCA.CLDS_RouteCableDetail(Me, "")
            Me.gbQBECFilter.Visible = True
            Me.gbQBECFilter.Location = Me.tcRoute.Location
            Me.cbQBERouted.Text = "-"
            Me.cbQBEArouted.Text = "-"
            Me.cbQBEOffroute.Text = "-"
            Me.tbQBESegment.Text = ""
            If pCA.pCMAPI.CMUnits = 1 Then
                Me.lbQBEVessel.Hide()
                Me.cbQBEVessel.Hide()
            Else
                Me.cbQBEVessel.Items.Clear()
                Me.cbQBEVessel.Items.Add("-")
                For i As Integer = 1 To pCA.pCMAPI.CMUnits
                    Me.cbQBEVessel.Items.Add(Trim(Str$(i)))
                Next
                Me.lbQBEVessel.Show()
                Me.cbQBEVessel.Show()
            End If
            Me.cbQBEVessel.Text = Trim(Str$(CA.pProps.v_focus))
            Me.lvCRSegments.Clear()
            If Not pSD Is Nothing Then
                If pSD.lvCables.SelectedItems.Count = 1 Then
                    Me.tbCabID.Text = pSD.lvCables.SelectedItems.Item(0).Text
                End If
            End If
            If Me.tbCabID.Text = "" Then
                Dim did As String
                Dim nid As String
                pCA.CLDS_PickDeviceNode(did, nid)
                If did <> "" And nid <> "" Then
                    Me.tbFromDev.Text = did
                    Me.tbFromNode.Text = nid
                Else
                    Dim sid As String = pCA.CLDS_PickedSegment
                    If sid <> "" Then
                        Me.tbQBESegment.Text = sid
                    End If
                End If
            End If
        Else
            Me.tbCabID.Text = qbeCCableID
            Me.tbFromDev.Text = qbeCDeviceID
            Me.tbFromNode.Text = qbeCNodeID
            Me.tbType.Text = qbeCType
            Me.tbSeg.Text = qbeCSeg
            Me.cbQBERouted.Text = qbeFRouted
            Me.cbQBEArouted.Text = qbeFARouted
            Me.cbQBEOffroute.Text = qbeFOffroute
            Me.cbQBEVessel.Text = qbeVessel
            Me.tbQBESegment.Text = qbeSegment
        End If
    End Sub

    Private Sub btARShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btARShow.Click
        Dim a As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.ARColour.R, Me.ARColour.G, Me.ARColour.B)
        Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
        pCA.CLDS_ARouteHilite(Me, Me.tbCabID.Text, Me.tbARNo.Text, a, c, Me.lvARSegments)
    End Sub
    Private Sub lvARSegments_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvARSegments.SelectedIndexChanged
        Dim lv As ListView = DirectCast(sender, ListView)
        If lv.SelectedItems.Count = 1 Then
            Dim lvi As ListViewItem = lv.SelectedItems.Item(0)
            CA.CLDS_SelectSegment(lvi.SubItems.Item(2).Text, lvi.SubItems.Item(3).Text)
        End If
    End Sub
    Public Sub SetNodes()
        If Me.rbARScopeW.Checked Then
            Me.tbARFnode.Text = Me.tbFromNode.Text
            Me.tbARTNode.Text = Me.tbToNode.Text
            If Me.tbARFnode.Text = "" Then
                Me.tbARFnode.Text = "@-FDEV-@"
            End If
            If Me.tbARTNode.Text = "" Then
                Me.tbARTNode.Text = "@-TDEV-@"
            End If
            Me.tbARFnode.ReadOnly = True
            Me.tbARTNode.ReadOnly = True
        End If
        If Me.rbARScopeP.Checked Then
            Me.tbARFnode.Text = ""
            Me.tbARTNode.Text = ""
            Me.tbARFnode.ReadOnly = False
            Me.tbARTNode.ReadOnly = False
        End If
        If Me.rbARScopeB.Checked Then
            Me.tbARFnode.Text = Me.tbFromNode.Text
            Me.tbARTNode.Text = Me.tbToNode.Text
            Me.tbARFnode.ReadOnly = True
            Me.tbARTNode.ReadOnly = True
        End If
    End Sub
    Public Sub SetScope()
        If Val(Me.tbNStatus.Text) = 0 Then
            Me.rbARScopeW.Checked = True
            Me.rbARScopeW.Enabled = True
            Me.rbARScopeB.Enabled = False
            Me.rbARScopeP.Enabled = True
        ElseIf Val(Me.tbNStatus.Text) >= CA.R_DRCNA Then
            Me.rbARScopeW.Checked = False
            Me.rbARScopeW.Enabled = False
            Me.rbARScopeP.Checked = False
            Me.rbARScopeP.Enabled = False
            Me.rbARScopeB.Checked = False
            Me.rbARScopeB.Enabled = False
        Else
            Me.rbARScopeW.Enabled = True
            Me.rbARScopeP.Enabled = True
            Me.rbARScopeB.Enabled = True
            If Me.tbFromNode.Text = "" Or Me.tbToNode.Text = "" Then
                Me.rbARScopeP.Checked = True
            Else
                Me.rbARScopeB.Checked = True
            End If
        End If
        SetNodes()
    End Sub
    Private Sub btARAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btARAccept.Click
        'If pARPrep Then
        If Me.tbARNo.Text = "" Then
            CA.CLDS_SubmitAR(Me)
            Me.tbARFnode.ReadOnly = True
            Me.tbARTNode.ReadOnly = True
            Me.gbARScope.Enabled = False
            Me.btARSubmit.Enabled = False
            pARPrep = False
        Else
            CA.CLDS_AcceptAR(Me, "UPDATE")
        End If
    End Sub

    Private Sub btARReject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btARReject.Click
        'If pARPrep Then
        If Me.tbARNo.Text = "" Then
            Me.tbARFnode.Text = ""
            Me.tbARFnode.ReadOnly = True
            Me.tbARTNode.Text = ""
            Me.tbARTNode.ReadOnly = True
            Me.gbARScope.Enabled = False
            pARPrep = False
        Else
            CA.CLDS_AcceptAR(Me, "DELETE")
        End If
    End Sub

    Private Sub btARColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btARColour.Click
        Dim result As DialogResult = Me.clCRColour.ShowDialog()
        If result = DialogResult.OK Then
            Me.ARColour = Me.clCRColour.Color
            Me.btARColour.BackColor = Me.ARColour
            Dim a As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.ARColour.R, Me.ARColour.G, Me.ARColour.B)
            Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
            pCA.CLDS_ARouteHilite(Me, Me.tbCabID.Text, Me.tbARNo.Text, a, c, Me.lvARSegments)
        End If
    End Sub
    Private Sub btARSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btARSubmit.Click
        Me.tbARFnode.Text = Me.tbFromNode.Text
        Me.tbARFnode.ReadOnly = False
        Me.tbARTNode.Text = Me.tbToNode.Text
        Me.tbARTNode.ReadOnly = False
        Me.gbARScope.Enabled = True
        Me.btARAccept.Enabled = True
        Me.btARReject.Enabled = True
        Me.tbARStatus.Text = ""
        SetScope()
        pARPrep = True
    End Sub
    Private Sub btCRDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRDelete.Click
        CA.CLDS_DeleteCR(Me, Me.tbCabID.Text, Me.lvCRSegments)
    End Sub
    Private Sub lvCRules_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvCRules.SelectedIndexChanged
        Dim lv As ListView = DirectCast(sender, ListView)
        If lv.SelectedItems.Count = 1 Then
            Dim lvi As ListViewItem = lv.SelectedItems.Item(0)
            Dim rt As String = lvi.Text
            If rt = "RN" Or rt = "PN" Then
                Dim rn As String = lvi.SubItems.Item(1).Text
                CA.CLDS_SelectNode(rn)
            ElseIf rt = "RS" Or rt = "PS" Or rt = "NS" Then
                Dim fn As String = lvi.SubItems.Item(5).Text
                Dim tn As String = lvi.SubItems.Item(6).Text
                CA.CLDS_SelectSegment(fn, tn)
            End If
        End If
    End Sub
    Private Sub btAddRRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAddRRule.Click
        CA.CLDS_AddReqdRule(Me, Me.tbCabID.Text)
    End Sub
    Private Sub btAddPRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAddPRule.Click
        CA.CLDS_AddBarredRule(Me, Me.tbCabID.Text)
    End Sub
    Private Sub btDelRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDelRule.Click
        CA.CLDS_DeleteCRule(Me)
    End Sub
    Private Sub btCommitRu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCommitRu.Click
        CA.CLDS_SaveCRules(Me)
    End Sub
    Private Sub btUpRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUpRule.Click
        CA.CLDS_UpdateCRule(Me, "UP")
    End Sub
    Private Sub btDownRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDownRule.Click
        CA.CLDS_UpdateCRule(Me, "DOWN")
    End Sub
    Private Sub tbThreshold_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbFillThreshold.TextChanged, tbLockThreshold.TextChanged
        Me.btClearPrefs.Enabled = True
        Me.btSavePrefs.Enabled = True
    End Sub
    Private Sub btDefaultPrefs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDefaultPrefs.Click
        CA.CLDS_DeleteRoutePrefs(Me, Me.tbCabID.Text)
    End Sub
    Private Sub cbARPartial_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbARPartial.CheckedChanged
        Me.tbFlags.Text = Trim(Str$(CA.CLDS_SetFlags(Me)))
        Me.btClearPrefs.Enabled = True
        Me.btSavePrefs.Enabled = True
    End Sub
    Private Sub cbIgnore_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles cbIHotCodes.CheckedChanged, cbIIsol.CheckedChanged, cbILocks.CheckedChanged, _
            cbIMBR.CheckedChanged, cbIMinMax.CheckedChanged, cbIProhib.CheckedChanged, _
            cbIReqd.CheckedChanged, cbISeg.CheckedChanged, cbIZones.CheckedChanged
        Me.tbIgnore.Text = Trim(Str$(CA.CLDS_SetIgnore(Me)))
        Me.btClearPrefs.Enabled = True
        Me.btSavePrefs.Enabled = True
    End Sub

    Private Sub btClearPrefs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClearPrefs.Click
        CA.CLDS_RoutePrefs(Me, Me.tbCabID.Text)
    End Sub

    Private Sub btSavePrefs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSavePrefs.Click
        CA.CLDS_SaveRoutePrefs(Me, Me.tbCabID.Text)
    End Sub

    Private Sub tbARNo_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbARNo.DoubleClick
        If Me.tbARNo.Text = "" Then
            Exit Sub
        End If
        Me.tbARLog.Text = ""
        If Me.tbARLog.Visible Then
            Me.tbARLog.Visible = False
        Else
            Dim doc As Autodesk.AutoCAD.ApplicationServices.Document
            Dim ed As Autodesk.AutoCAD.EditorInput.Editor
            doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            ed = doc.Editor
            Me.tbARLog.Visible = True
            CA.ShowLogCLOB(ed, CA.pNetDialog.Context, "JOB=""" & Me.tbARNo.Text & """", Me.tbARLog)
        End If
    End Sub
    Private Sub rbARScope_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbARScopeW.CheckedChanged, rbARScopeP.CheckedChanged, rbARScopeB.CheckedChanged
        SetNodes()
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        If rb.Name = "rbARScopeP" And rb.Checked Then
            Dim n1 As String = "", n2 As String = ""
            CA.CLDS_PickTwoNodes(n1, n2)
            Me.tbARFnode.Text = n1
            Me.tbARFnode.Update()
            Me.tbARTNode.Text = n2
            Me.tbARTNode.Update()
        End If
    End Sub

    Private Sub btMRStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMRStart.Click
        Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
        pCA.CLDS_RouteHilite(Me, Me.tbCabID.Text, c, Me.lvCRSegments)
        Me.tbMRStart.Text = ""
        Me.tbMRLength.Text = Me.tbCRLength.Text
        Me.lvMRNext.Clear()
        If Val(Me.tbNStatus.Text) >= pCA.R_DRCNA Then
            Me.tbMRStart.ReadOnly = True
            Exit Sub
        End If
        Me.tbMRStart.ReadOnly = False
        Me.tbMRStart.Focus()
        CA.CLDS_SetMRStart(Me)
    End Sub

    Private Sub btMRGoNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMRGoNext.Click
        If Me.tbMRStart.Text = "" Then
            MsgBox("No start node specified", MsgBoxStyle.Exclamation, "CMPIC Manual Route")
            Exit Sub
        End If
        CA.CLDS_GetMRNext(Me)
    End Sub

    Private Sub lvMRNext_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvMRNext.ItemChecked
        Dim lv As ListView = DirectCast(sender, ListView)
        Dim lvi As ListViewItem = DirectCast(e.Item, ListViewItem)
        If lvi.Checked Then
            Dim x As Integer = Val(lvi.SubItems(6).Text)
            If x < 0 Then
                lvi.Checked = False
            Else
                CA.CLDS_SetMRNext(Me, lvi.Text, lvi.SubItems(1).Text)
            End If
        End If
    End Sub

    Private Sub lvMRNext_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvMRNext.SelectedIndexChanged
        Dim fn As String = Me.tbMRStart.Text
        Dim lv As ListView = DirectCast(sender, ListView)
        If lv.SelectedItems.Count = 1 Then
            Dim lvi As ListViewItem = lv.SelectedItems.Item(0)
            Dim tn As String = lvi.Text
            CA.CLDS_SelectSegment(fn, tn, False)
        End If
    End Sub

    Private Sub tbMRRewind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbMRRewind.Click
        CA.CLDS_DelMRPrev(Me)
    End Sub

    Private Sub tbFromDev_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbFromDev.MouseDoubleClick
        CA.CLDS_SelectNode("", Me.tbFromDev.Text, True)
    End Sub


    Private Sub tbFromNode_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbFromNode.MouseDoubleClick
        CA.CLDS_SelectNode(Me.tbFromNode.Text, "", True)
    End Sub

    Private Sub tbToNode_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbToNode.MouseDoubleClick
        CA.CLDS_SelectNode(Me.tbToNode.Text, "", True)
    End Sub

    Private Sub tbToDev_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbToDev.MouseDoubleClick
        CA.CLDS_SelectNode("", Me.tbToDev.Text, True)
    End Sub

    Private Sub btCabEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCabEdit.Click
        '        CA.CLDS_EditCable(Me, Me.tbCabID.Text)
        CA.CLDS_SetComboBox(Me.cbFNode, Me.tbFromNode.Text, "DV", "NX", "ID", Me.tbFromDev.Text)
        Me.cbFNode.Show()
        CA.CLDS_SetComboBox(Me.cbTNode, Me.tbToNode.Text, "DV", "NX", "ID", Me.tbToDev.Text)
        Me.cbTNode.Show()
        CA.CLDS_SetComboBox(Me.cbCType, Me.tbType.Text, "NS", "PR", "MUN", "%")
        Me.cbCType.Show()
        CA.CLDS_SetComboBox(Me.cbCSeg, Me.tbSeg.Text, "SC", "CB", "SEGREGATION", "%")
        Me.cbCSeg.Show()
        Me.tbFOffL.ReadOnly = False
        Me.tbTOffL.ReadOnly = False
    End Sub

    Private Sub btCabSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCabSave.Click
        If CA.CLDS_SaveCable(Me) Then
            Me.cbFNode.Hide()
            Me.cbTNode.Hide()
            Me.cbCType.Hide()
            Me.cbCSeg.Hide()
            Me.tbFOffL.ReadOnly = True
            Me.tbTOffL.ReadOnly = True
            pCA.CLDS_RouteCableDetail(Me, Me.tbCabID.Text)
        End If
    End Sub

    Private Sub btCRReverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRReverse.Click
        CA.CLDS_ReverseRoute(Me)
        Dim c As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(Me.CRColour.R, Me.CRColour.G, Me.CRColour.B)
        pCA.CLDS_RouteCableDetail(Me, Me.tbCabID.Text, Me.Vessel)
        pCA.CLDS_RouteHilite(Me, Me.tbCabID.Text, c, Me.lvCRSegments, Me.Vessel)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cxRuleset.SelectedIndexChanged
        Dim cb As ComboBox = TryCast(sender, ComboBox)
        Dim rs As Integer = Val(cb.Text)
        Dim mr As CLDS_AR_Multi = CA.M_RULESET.Item(rs)
        Me.tbARNo.Text = Trim(Str$(mr.RRINO))
        Me.tbARLength.Text = Trim(Str$(mr.RRLength))
        Me.tbARStatus.Text = mr.RRComplete
        Me.btARShow.Enabled = False
        Me.cbARApprove.Enabled = False
        Me.btARAccept.Enabled = False
        Me.btARReject.Enabled = False
        If Me.tbARNo.Text <> "" Then
            If rs = 0 Then
                Me.btARReject.Enabled = True
                If Mid$(Me.tbARStatus.Text, 1, 1) = "Y" Or Mid$(Me.tbARStatus.Text, 1, 1) = "P" Or Mid$(Me.tbARStatus.Text, 1, 1) = "M" Then
                    Me.btARShow.Enabled = True
                    Me.btARAccept.Enabled = True
                End If
                If Mid$(Me.tbARStatus.Text, 1, 1) = "Y" Then
                    Me.cbARApprove.Enabled = True
                End If
            Else
                If Mid$(Me.tbARStatus.Text, 1, 1) = "Y" Or Mid$(Me.tbARStatus.Text, 1, 1) = "P" Or Mid$(Me.tbARStatus.Text, 1, 1) = "M" Then
                    Me.btARShow.Enabled = True
                End If
            End If
        End If
        CA.CLDS_ARoutePrefs(Me, rs)
    End Sub
    Private Sub numericTextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles tbFOffL.KeyPress, tbTOffL.KeyPress
        Dim tb As TextBox = CType(sender, TextBox)
        Dim chr As Char = e.KeyChar
        'If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
        If IsNumeric(e.KeyChar) Then
            'If adding the character to the end of the current TextBox value results in
            ' a numeric value, go on. Otherwise, set e.Handled to True, and don't let
            ' the character to be added.
            e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
        ElseIf e.KeyChar = "." Then
            'For the decimal character (.) we need a different rule:
            'If adding a decimal to the end of the current value of the TextBox results
            ' in a numeric value, it can be added. If not, this means we already have a
            ' decimal in the TextBox value, so we only allow the new decimal to sit in
            ' when it is overwriting the previous decimal.
            If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
                e.Handled = True
            End If
        ElseIf e.KeyChar = "-" Then
            If UCase(tb.Name) = "TBDZ" Then ' Only elevation field allows negative values
                'A negative sign is prevented if the "-" key is pressed in any location
                ' other than the begining of the number, or if the number already has a
                ' negative sign
                If tb.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "-" Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        ElseIf Not Char.IsControl(e.KeyChar) Then
            'IsControl is checked, because without that, keys like BackSpace couldn't
            ' work correctly.
            e.Handled = True
        End If
        If Not e.Handled Then
            Me.btCabSave.Enabled = True
        End If
    End Sub
End Class
